package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsImg;
import cn.ctodb.cms.repository.CmsImgRepository;
import cn.ctodb.cms.repository.search.CmsImgSearchRepository;
import cn.ctodb.cms.service.dto.CmsImgDTO;
import cn.ctodb.cms.service.mapper.CmsImgMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsImg.
 */
@Service
@Transactional
public class CmsImgService {

    private final Logger log = LoggerFactory.getLogger(CmsImgService.class);
    
    @Inject
    private CmsImgRepository cmsImgRepository;

    @Inject
    private CmsImgMapper cmsImgMapper;

    @Inject
    private CmsImgSearchRepository cmsImgSearchRepository;

    /**
     * Save a cmsImg.
     *
     * @param cmsImgDTO the entity to save
     * @return the persisted entity
     */
    public CmsImgDTO save(CmsImgDTO cmsImgDTO) {
        log.debug("Request to save CmsImg : {}", cmsImgDTO);
        CmsImg cmsImg = cmsImgMapper.cmsImgDTOToCmsImg(cmsImgDTO);
        cmsImg = cmsImgRepository.save(cmsImg);
        CmsImgDTO result = cmsImgMapper.cmsImgToCmsImgDTO(cmsImg);
        cmsImgSearchRepository.save(cmsImg);
        return result;
    }

    /**
     *  Get all the cmsImgs.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsImgDTO> findAll() {
        log.debug("Request to get all CmsImgs");
        List<CmsImgDTO> result = cmsImgRepository.findAll().stream()
            .map(cmsImgMapper::cmsImgToCmsImgDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsImg by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsImgDTO findOne(Long id) {
        log.debug("Request to get CmsImg : {}", id);
        CmsImg cmsImg = cmsImgRepository.findOne(id);
        CmsImgDTO cmsImgDTO = cmsImgMapper.cmsImgToCmsImgDTO(cmsImg);
        return cmsImgDTO;
    }

    /**
     *  Delete the  cmsImg by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsImg : {}", id);
        cmsImgRepository.delete(id);
        cmsImgSearchRepository.delete(id);
    }

    /**
     * Search for the cmsImg corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsImgDTO> search(String query) {
        log.debug("Request to search CmsImgs for query {}", query);
        return StreamSupport
            .stream(cmsImgSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsImgMapper::cmsImgToCmsImgDTO)
            .collect(Collectors.toList());
    }
}
